<?php

namespace lc\activity\model;

use think\Db;
use think\Model;

class LotterySmsConfig extends Model
{
	protected $table = 'lottery_sms_configs';
    protected $pk = 'ls_id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ls_create_at';
    protected $updateTime = 'ls_update_at';

	/**
	 * @desc    生成APPID
	 * @return  false|string
	 */
	public static function generateAppId()
	{
		$str   = date('Y', time());
		$chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

		for ($i = 1; $i <= 8; $i++) {
			$j = rand(0, count($chars) - 1);
			$str .= $chars[$j];
			array_splice($chars, $j, 1);
		}
		return 'sms'.$str;
	}

	/**
	 * @desc    生成SECRET
	 * @param   $appId
	 * @return  string
	 */
	public static function generateAppSecret($appId)
	{
		return sha1($appId);
	}

	public static function createTableSmsCode($id)
	{
		Db::statement("DROP TABLE IF EXISTS `g_{$id}_smscode`");
		$sql = "
            CREATE TABLE `g_{$id}_smscode` (
              `s_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `s_phone` varchar(20) NOT NULL COMMENT '手机号码',
              `s_content` varchar(100) NOT NULL COMMENT '短信内容',
              `s_used` tinyint(1) unsigned DEFAULT '0',
              `s_expire_at` datetime DEFAULT NULL,
              `s_create_at` datetime DEFAULT NULL,
              PRIMARY KEY (`s_id`),
              KEY `s_phone` (`s_phone`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        ";
		Db::statement($sql);
	}
}